package com.zaofan.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zaofan.bean.AppyLog;
import com.zaofan.bean.AppyOpen;
import com.zaofan.bean.OpenCity;
import com.zaofan.bean.SmsLog;
import com.zaofan.constant.StateConstant;
import com.zaofan.constant.TypeConstant;
import com.zaofan.dao.AppyOpenDao;
import com.zaofan.exception.ServiceException;
import com.zaofan.utils.SendSms;
import com.zaofan.utils.WebUtils;

public class AppyOpenService {
	private AppyOpenDao appyOpenDao = new AppyOpenDao();
	private OpenCityService cityService = new OpenCityService();
	private SmsLogService smsLogService = new SmsLogService();
	private AppyLogService appylogService = new AppyLogService();

	public void delete(AppyOpen appyOpen) {
		appyOpenDao.del(appyOpen);
	}

	public Integer add(AppyOpen appyOpen) {
		appyOpen.setAddTime(new Date());
		return appyOpenDao.addAppyOpen(appyOpen);
	}

	public void update(AppyOpen appyOpen) {
		appyOpenDao.update(appyOpen);
	}

	public List<AppyOpen> findAll(Map<String, Object> params) {
		List<AppyOpen> data = appyOpenDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return appyOpenDao.count(params);
	}

	public AppyOpen findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return appyOpenDao.findOne(params);
	}

	public void addAppy(AppyOpen appyOpen) throws ServiceException {
		appyOpen.setAddTime(new Date());
		if (appyOpen.getCityId() < 1) {
			throw new ServiceException("城市信息错误!!");
		}
		OpenCity city = cityService.findById(appyOpen.getCityId());
		if (city == null) {
			throw new ServiceException("城市信息错误!!");
		}
		appyOpen.setCityName(city.getCityName());
		appyOpen.setMobile("");
		appyOpenDao.add(appyOpen);
	}

	public void setState(AppyOpen appyOpen) throws ServiceException {
		if (WebUtils.isEmpty(appyOpen.getCityName())) {
			throw new ServiceException("城市信息错误!!");
		}
		OpenCity city = cityService.findByCityName(appyOpen.getCityName());
		if (city == null) {
			throw new ServiceException("城市信息错误!!");
		}
		appyOpen.setCityId(city.getId());
		appyOpen.setCityName(city.getCityName());
		appyOpenDao.setState(appyOpen);
	}

	public void sendValidateCode(String mobile, String code) throws ServiceException {
		if (WebUtils.isEmpty(mobile)) {
			throw new ServiceException("请填写正确的手机号码!!");
		}
		if (!WebUtils.isMobileNO(mobile)) {
			throw new ServiceException("手机号码格式不正确!!");
		}
		Long count = smsLogService.CountTodayLogs(mobile);
		if (count > 3) {
			throw new ServiceException("对不起,您的号码发送多次,请明天再试吧!!");
		}
		int result = SendSms.sendRegisterCode(mobile, code);
		if (result == 2) {
			SmsLog log = new SmsLog();
			log.setCode(code);
			log.setMobile(mobile);
			log.setSendTime(new Date());
			log.setType(TypeConstant.SEND_SMS_TYPE_VALIDATE_MOBILE);
			smsLogService.add(log);
		} else {
			throw new ServiceException("对不起,您的号码有误,请售后再试!!");
		}
	}

	public void addUserAppy(AppyOpen appyOpen) throws ServiceException {
		if (!WebUtils.isMobileNO(appyOpen.getMobile())) {
			throw new ServiceException("请填写正确的手机号码!!");
		}
		if (WebUtils.isEmpty(appyOpen.getBuilderName())) {
			throw new ServiceException("大楼信息不能为空!!");
		}
		if (WebUtils.isEmpty(appyOpen.getCityName())) {
			throw new ServiceException("城市信息不能为空!!");
		}
		if (WebUtils.isEmpty(appyOpen.getCompanyName())) {
			throw new ServiceException("公司信息不能为空!!");
		}
		appyOpen.setCount(1);
		appyOpen.setState(StateConstant.APPY_STATE_CHECKED);
		appyOpen.setMark("");
		Integer appyId = add(appyOpen);
		AppyLog appylog = new AppyLog();
		appylog.setAppyId(appyId);
		appylog.setMobile(appyOpen.getMobile());
		appylogService.add(appylog);
	}

	public void appy(String id, String mobile) throws ServiceException {
		if (!WebUtils.isMobileNO(mobile)) {
			throw new ServiceException("请填写正确的手机号码!!");
		}
		Integer appyId = 0;
		try {
			appyId = Integer.valueOf(id);
		} catch (Exception e) {
		}
		AppyOpen open = findById(appyId);
		if (open == null) {
			throw new ServiceException("请填写正确的手机号码!!");
		}
		AppyLog log = appylogService.exit(appyId, mobile);
		if (log != null) {
			throw new ServiceException("对不起,你已经参与了该大楼了!!");
		}
		AppyLog appylog = new AppyLog();
		appylog.setAppyId(appyId);
		appylog.setMobile(mobile);
		appylog.setAddTime(new Date());
		appylogService.add(appylog);
		open.setCount(open.getCount() + 1);
		update(open);
	}

	public List<AppyOpen> findByNameLike(String key) {
		List<AppyOpen> opens = appyOpenDao.findByNameLike(key);
		if (opens == null) {
			opens = new ArrayList<>();
		}
		return opens;
	}

	@SuppressWarnings("unchecked")
	public List<AppyLog> findLogs(String id_s) throws ServiceException {
		Integer id;
		try {
			id = Integer.valueOf(id_s);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			throw new ServiceException("参数错误!!");
		}
		AppyOpen appyOpen = this.findById(id);
		if (appyOpen == null) {
			throw new ServiceException("参数错误!!");
		}
		List<AppyLog> logs = appylogService.findByAppyId(appyOpen.getId());
		if (logs == null) {
			return Collections.EMPTY_LIST;
		}
		return logs;
	}

}
